{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: Contract Type Classification using LabelConcept"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: Contract Type Classification using LabelConcept\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Document, DocumentLLM, LabelConcept\n",
        "\n",
        "\n",
        "# Create a Document object from legal document text\n",
        "legal_doc_text = \"\"\"\n",
        "NON-DISCLOSURE AGREEMENT\n",
        "\n",
        "This Non-Disclosure Agreement (\"Agreement\") is entered into as of January 15, 2025, by and between TechCorp Inc., a Delaware corporation (\"Disclosing Party\"), and DataSystems LLC, a California limited liability company (\"Receiving Party\").\n",
        "\n",
        "WHEREAS, Disclosing Party possesses certain confidential information relating to its proprietary technology and business operations;\n",
        "\n",
        "NOW, THEREFORE, in consideration of the mutual covenants contained herein, the parties agree as follows:\n",
        "\n",
        "1. CONFIDENTIAL INFORMATION\n",
        "The term \"Confidential Information\" shall mean any and all non-public information...\n",
        "\n",
        "2. OBLIGATIONS OF RECEIVING PARTY\n",
        "Receiving Party agrees to hold all Confidential Information in strict confidence...\n",
        "\"\"\"\n",
        "\n",
        "doc = Document(raw_text=legal_doc_text)\n",
        "\n",
        "# Define a LabelConcept for contract type classification\n",
        "contract_type_concept = LabelConcept(\n",
        "    name=\"Contract Type\",\n",
        "    description=\"Classify the type of contract\",\n",
        "    labels=[\"NDA\", \"Consultancy Agreement\", \"Privacy Policy\", \"Other\"],\n",
        "    classification_type=\"multi_class\",  # only one label can be selected (mutually exclusive labels)\n",
        "    singular_occurrence=True,  # expect only one classification result\n",
        ")\n",
        "print(contract_type_concept._format_labels_in_prompt)\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([contract_type_concept])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept from the document\n",
        "contract_type_concept = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Check if any labels were extracted\n",
        "if contract_type_concept.extracted_items:\n",
        "    # Get the classified document type\n",
        "    classified_type = contract_type_concept.extracted_items[0].value\n",
        "    print(f\"Document classified as: {classified_type}\")  # Output: ['NDA']\n",
        "else:\n",
        "    print(\"No applicable labels found for this document\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}